package com.ugoodtech.mdcc.web.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.utils.ComplaintReportUtil;
import com.ugoodtech.mdcc.web.dto.ComplaintSolutionVO;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.service.ComplaintSolutionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class}, propagation = Propagation.REQUIRED)
public class ComplaintSolutionServiceImpl implements ComplaintSolutionService {

    /**
     * 投诉解决方案-Repository
     */
    @Autowired
    private ComplaintSolutionReportRepository complaintSolutionReportRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private ComplaintSolutionTimeOutRepository complaintSolutionTimeOutRepository;

    @Autowired
    private ComplaintSolutionDataRepository complaintSolutionDataRepository;


    /**
     * 获取投诉方案解决列表
     *
     * @param complaintReprotId
     * @param pageable
     * @return
     */
    @Override
    public JsonResponse getComplaintSolutionList(Long complaintReprotId, Pageable pageable) {
        QComplaintSolutionReport qComplaintSolutionReport = QComplaintSolutionReport.complaintSolutionReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qComplaintSolutionReport.deleted.eq(false));
        builder.and(qComplaintSolutionReport.enabled.eq(true));
        builder.and(qComplaintSolutionReport.complaintReport.id.eq(complaintReprotId));
        Page<ComplaintSolutionReport> complaintSolutionReportPage = complaintSolutionReportRepository.findAll(builder, pageable);
        List<ComplaintSolutionVO> auditList = getResponse(complaintSolutionReportPage.getContent());
        Page<ComplaintSolutionVO> page = new PageImpl(auditList, pageable, complaintSolutionReportPage.getTotalElements());
        return JsonResponse.successResponseWithPageData(page);
    }

    /**
     * 封装返回类
     *
     * @param complaintSolutionReportList
     * @return
     */
    private List<ComplaintSolutionVO> getResponse(List<ComplaintSolutionReport> complaintSolutionReportList) {
        List<ComplaintSolutionVO> responseList = new ArrayList<>();
        for (ComplaintSolutionReport report : complaintSolutionReportList) {
            ComplaintSolutionVO vo = new ComplaintSolutionVO();
            //设置解决方案报告ID
            vo.setComplaintSolutionReportId(report.getId());
            //设置解决方案创建人
            UserProfile userProfile = userProfileRepository.findByUserId(report.getUser().getId());
            if (null == userProfile || StringUtils.isEmpty(userProfile.getRealName())) {
                continue;
            }
            vo.setReportUserName(userProfile.getRealName());
            //设置日期
            ComplaintSolutionData complaintSolutionData = complaintSolutionDataRepository.selectSolutionDate(report.getId(), 200L);
            if (null != complaintSolutionData && StringUtils.isNotEmpty(complaintSolutionData.getAnswerOther())) {
                vo.setCreateTimeStr(complaintSolutionData.getAnswerOther());
            } else {
                vo.setCreateTimeStr(report.getUpdateTimeStr());
            }
            //设置填写的解决状态
            vo.setStatus(ComplaintReportUtil.getReportStatus(report.getReportType()));
            //查询此解决方案是否为超时的解决方案
            List<ComplaintSolutionTimeOut> complaintSolutionTimeOut = complaintSolutionTimeOutRepository.findBySolutionId(report.getId());
            if (!complaintSolutionTimeOut.isEmpty()) {
                vo.setOrange(true);
            }
            responseList.add(vo);
        }
        return responseList;
    }


}
